package com.ifar.audiosdk.broadcast

import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import com.ifar.audiosdk.common.IfarEventBusTag
import com.orhanobut.logger.Logger
import org.simple.eventbus.EventBus

/**
 * 网络切换回调
 */
class NetworkCallbackImpl : ConnectivityManager.NetworkCallback() {
    override fun onAvailable(network: Network) {
        super.onAvailable(network)
        EventBus.getDefault().post("",IfarEventBusTag.NET_WORK_CHANGE)
        Logger.e("网络已链接")
    }

    override fun onLost(network: Network) {
        super.onLost(network)
        Logger.e("网络已断开")
    }

    override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities)
        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            when {
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
        //                Logger.e("wifi已经连接");
                }
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
        //                Logger.e("数据流量已经连接");
                }
                else -> {
        //                Logger.e("其他网络");
                }
            }
        }
    }
}